#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

bool is_game_over = false;

typedef struct
{
    int blood;
    char role;
    vector<char> cards;
    bool zhuge;
    bool is_used_kill;
    int tag; //0:none 1:zhong 2:fanzei-like 3:fanzei
    int sit_index;
} Player;

// 查找一张卡牌，返回卡牌下标，找不到返回-1
int find_card(vector<Player>::iterator player, char target)
{
    int i;
    for (i = 0; i < player->cards.size(); i++)
    {
        if (player->cards[i] == target)
        {
            return i;
        }
    }
    return -1;
}

// 测试立场，敌对返回0，中立返回1，友好返回2
int test_attack(vector<Player>::iterator player1, vector<Player>::iterator player2)
{
    if (player1->role == 'M')
    {
        switch (player2->tag)
        {
            case 0:
                return 1;
            case 1:
                return 2;
            case 2:
                return 0;
            case 3:
                return 0;
        }
    }
    else if (player1->role == 'Z')
    {
        if (player2->role == 'M')
        {
            return 2;
        }
        
        switch (player2->tag)
        {
            case 0:
                return 1;
            case 1:
                return 2;
            case 2:
                return 1;
            case 3:
                return 0;
        }
    }
    else if (player1->role == 'F')
    {
        if (player2->role == 'M')
        {
            return 0;
        }
        
        switch (player2->tag)
        {
            case 0:
                return 1;
            case 1:
                return 0;
            case 2:
                return 1;
            case 3:
                return 2;
        }
    }
    
}

// from对to扣一滴血，游戏结束返回true
bool attack(vector<Player>::iterator from, vector<Player>::iterator to, queue<char> *queues, vector<Player> *players)
{
    int i;
    to->blood--;
    if (to->blood > 0)
    {
        return;
    }
    for (i = 0; i < to->cards.size(); i++)
    {
        if (to->cards[i] == 'P')
        {
            to->cards.erase(to->cards.begin() + i);
            to->blood++;
            return;
        }
    }
    if (to->role == 'F')
    {
        from->cards.push_back(queues->front());
        queues->pop();
        from->cards.push_back(queues->front());
        queues->pop();
        from->cards.push_back(queues->front());
        queues->pop();
    }
    else if (from->role == 'M' && to->role == 'Z')
    {
        from->cards.clear();
        from->zhuge = false;
    }

    players->erase(to);
}

int main(int argc, char const *argv[])
{
    string strtemp;
    char temp;
    int n, m, i, j, a, b, c;
    vector<Player> players;
    queue<char> queues;
    Player player_temp;
    vector<Player>::iterator round, target;

    cin >> n >> m;
    for (i = 0; i < n; i++)
    {
        player_temp.blood = 4;
        player_temp.zhuge = false;
        player_temp.is_used_kill = false;
        players.push_back(player_temp);
    }
    
    for (i = 0; i < n; i++)
    {
        players[i].sit_index = i;
        cin >> players[i].role;
        cin >> temp;
        for (j = 0; j < 4; j++)
        {
            cin >> temp;
            players[i].cards.push_back(temp);
        }
    }
    
    for (i = 0; i < m; i++)
    {
        cin >> temp;
        queues.push(temp);
    }

    round = players.begin();
    while (true)
    {
        // 摸牌
        round->cards.push_back(queues.front());
        queues.pop();
        round->cards.push_back(queues.front());
        queues.pop();
        
        // 出牌
        for (i = 0; i < round->cards.size(); i++)
        {
            switch (round->cards[i])
            {
                case 'P':
                    if (round->blood != 4)
                    {
                        round->blood++;
                        round->cards.erase(round->cards.begin() + i);
                        i--;
                    }
                    break;
                case 'K':
                    if (!player_temp.is_used_kill || player_temp.zhuge)
                    {
                        if (round + 1 == players.end())
                        {
                            target = players.begin();
                        }
                        else
                        {
                            target = round + 1;
                        }
                        
                        if (test_attack(round, target) == 0)
                        {
                            round->cards.erase(round->cards.begin() + i);
                            i--;
                            
                            a = find_card(target, 'D');
                            if (a != -1)
                            {
                                target->cards.erase(target->cards.begin() + a);
                            }
                            else
                            {
                                attack(round, target, &queues, &players);
                            }
                            break;
                        }

                        if (round == players.begin())
                        {
                            target = round + players.size() - 1;
                        }
                        else
                        {
                            target = round - 1;
                        }

                        if (test_attack(round, target) == 0)
                        {
                            round->cards.erase(round->cards.begin() + i);
                            i--;

                            a = find_card(target, 'D');
                            if (a != -1)
                            {
                                target->cards.erase(target->cards.begin() + a);
                            }
                            else
                            {
                                attack(round, target, &queues, &players);
                            }

                            break;
                        }
                    }
                    break;
                case 'D':
                    break;
                case 'F':
                    for (j = 0; j < players.size() - 1; j++)
                    {
                        target = &players[(round + j) % players.size()];
                        if (test_attack(round, target) == 0)
                        {
                            round->cards.erase(round->cards.begin() + i);
                            i--; 
                            while (true)
                            {
                                a = find_card(target, 'K');
                                if (a != -1)
                                {
                                    target->cards.erase(target->cards.begin() + a);
                                }
                                else
                                {
                                    attack(round, round, );
                                    break;
                                }
                            }
                        }
                    }
                    break;
                case 'N':
                /* code */
                    break;
                case 'W':
                /* code */
                    break;
                case 'J':
                /* code */
                    break;
                case 'Z':
                /* code */
                    break;
                default:
                    cout << "\ninput error\n";
                    break;
            }
        }
        
        player_temp.is_used_kill = false;

        round++;
        if (round == players.end())
        {
            round = players.begin();
        }
    }
    

    // for (i = 0; i < n; i++)
    // {
    //     cout << players[i].role << ":";
    //     for (j = 0; j < 4; j++)
    //     {
    //         cout << players[i].cards[j];
    //     }
    //     cout << endl;
    // }

    // for (i = 0; i < m; i++)
    // {
    //     cout << queues.front();
    //     queues.pop();
    // }
    
    return 0;
}
